package org.contextsensitivetodo.context;

import java.util.ArrayList;

import org.contextsensitivetodo.R;
import org.contextsensitivetodo.contextdetails.ViewUserContextDetailsActivity;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class UserContextAdapter extends ArrayAdapter<UserContext> {

	private ArrayList<UserContext> userContextItems;
	private Context context;
	
    public UserContextAdapter(Context context, int textViewResourceId, ArrayList<UserContext> items) {
            super(context, textViewResourceId, items);
            this.userContextItems = items;
            this.context = context;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.row, null);
            }
            final UserContext o = userContextItems.get(position);
            if (o != null) {
                    TextView tt = (TextView) v.findViewById(R.id.toptext);
                    TextView bt = (TextView) v.findViewById(R.id.bottomtext);
                    ImageView icon = (ImageView) v.findViewById(R.id.icon);
                    if (tt != null) {
                          tt.setText(o.getName());                            }
                    if(bt != null){
                    	if(o.isOn()){
                    		bt.setText("This rule is active.");
                    		icon.setImageResource(R.drawable.check);
                    	}else{
                    		bt.setText("");
                    		icon.setImageResource(R.drawable.cancel);
                    	}
                          
                    }
            }
            v.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					Log.d("cstodo", "on click event in adapter");
					Intent viewDetailsIntent = new Intent(context, ViewUserContextDetailsActivity.class);
					viewDetailsIntent.putExtra("usercontext", o.getName());
					context.startActivity(viewDetailsIntent);
		
				}
			});
            return v;
    }

	
}
